package com.guoguo.service.impl;

import com.github.pagehelper.PageHelper;
import com.guoguo.base.PageBean;
import com.guoguo.entity.Groups;
import com.guoguo.entity.Menus;
import com.guoguo.entity.vo.GroupVO;
import com.guoguo.mapper.GroupsMapper;
import com.guoguo.service.GroupsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by KLSD on 2017/10/23.
 */
@Service
public class GroupsServiceImpl implements GroupsService{

    @Autowired
    private GroupsMapper groupsMapper;

    @Override
    public PageBean<GroupVO> selectgroupList(int pageNumber,int pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        return new PageBean<GroupVO>(groupsMapper.selectgroupList());

    }

    @Override
    public List<Groups> getgroupList() {
        return groupsMapper.selectAll();
    }

    @Override
    public PageBean<GroupVO> selectBySortDesc(String groupName, Integer isShow, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageBean(groupsMapper.selectBySortDesc(groupName,isShow));
    }

    @Override
    public PageBean<GroupVO> selectBySortAsc(String groupName, Integer isShow,  int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageBean(groupsMapper.selectBySortAsc(groupName,isShow));
    }

    @Override
    public PageBean<GroupVO> selectByCreateTimeAsc(String groupName, Integer isShow, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageBean(groupsMapper.selectByCreateTimeAsc(groupName,isShow));
    }

    @Override
    public PageBean<GroupVO> selectByCreateTimeDesc(String groupName, Integer isShow,  int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageBean(groupsMapper.selectByCreateTimeDesc(groupName,isShow));
    }

    @Override
    public Groups selectByPrimaryKey(String groupId) {
        return groupsMapper.selectByPrimaryKey(groupId);
    }

    @Override
    public Groups addGroup(String groupId, String groupName, String groupDescription, int groupType, Date createTime, Date updateTime) {
        Groups groups =groupsMapper .selectByPrimaryKey(groupId) ;
        if(groups==null){
            groups=new Groups(groupId,groupName,groupDescription,groupType,createTime,updateTime);
            groups.setIsShow(1);
            int num = groupsMapper.insertSelective(groups);
            return groups;
        }else{
            groups.setGroupName(groupName );
            groups.setGroupDescription(groupDescription ) ;
            groups.setGroupType(groupType ) ;
            groups .setUpdateTime(updateTime ) ;
            int num=groupsMapper .updateByPrimaryKeySelective(groups ) ;
            return groups ;
        }
    }
}
